<?php


namespace app\models\store;

use app\models\system\SystemStore;
use app\models\user\UserCar;
use basic\BaseModel;
use think\facade\Db;
use traits\ModelTrait;
use utils\Json;

/**
 * 洗车的订单表
 * Class StoreOrderCar
 * @package app\models\store
 */
class StoreOrderCar extends BaseModel
{
    /**
     * 使用基础的模型
     */
    use ModelTrait;

    /**
     * 模型名称
     * @var string
     */
    protected $name = "store_order_car";


    /**
     *
     * @var string
     */
    protected $pk = "id";

    /**
     * 获取所有的订单
     * @param int $uid 用户的ID
     * @param int|string $type 订单的类型    1 => 订单进行中 2 => 订单取消 3 => 订单完成 4 => 等待中 5 其他
     * @param int $page 分页
     * @param int $limit 限制
     * @return array
     */
    public static function getUserOrderSearchList($uid, $type, $page = 0, $limit = 8)
    {
        if ($type) $model = self::where("status", $type);
        else
            $model = new self();
        if ($page) $list = $model->where('wxapp_id', self::$wxapp_id)->where("is_del", 0)
            ->with(["store", "coupon", "car"])
            ->where("uid", $uid)->order("add_time DESC")->page((int)$page, (int)$limit)->select()->toArray();
        else
            $list = $model->where('wxapp_id', self::$wxapp_id)->where("is_del", 0)
                ->with(["store", "coupon", "car"])
                ->where("uid", $uid)->order("add_time DESC")->page((int)$page, (int)$limit)->select()->toArray();
        foreach ($list as &$item) {
            $item["add_time"] = date("Y-m-d H:i:s", $item["add_time"]);
        }
        // 获取订单的总数
        $total = self::where("uid", $uid)->where("is_del", 0)->count();
        return ["list" => $list, "total" => $total];
    }

    /**
     * 获取订单的列表
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function OrderListCar(array $where)
    {
        $model = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->field('a.*,r.nickname,r.phone,r.spread_uid')->where("a.wxapp_id", self::$wxapp_id);
        if ($where['order'] != '') {
            $model = $model->order(self::setOrder($where['order']));
        } else {
            $model = $model->order('a.id desc');
        }
        if (isset($where['excel']) && $where['excel'] == 1) {
            $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
        } else {
            $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
        }

        foreach ($data as &$item) {

            $_info =  [];

            $item['_info'] = $_info;
            $item['spread_nickname'] = Db::name('user')->where('uid', $item['spread_uid'])->value('nickname');
            $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            if ($item["pay_type"] == 10) {
                $item["pay_type_name"] = "微信支付";
            } elseif ($item["pay_type"] == 20) {
                $item["pay_type_name"] = "积分支付";
            } else {
                $item["pay_type_name"] = "洗车券扣除";
            }
        }
        if (isset($where['excel']) && $where['excel'] == 1) {
            self::SaveExcel($data);
        }
        $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->where("a.wxapp_id", self::$wxapp_id)->count();
        return compact('count', 'data');
    }


    /**
     * 一对一模型
     * @return \think\model\relation\HasOne
     */
    public function store()
    {
        return $this->hasOne(SystemStore::class, "id", "store_id")->field("id, name, detailed_address");
    }

    /**
     * 一对一模型
     * @return \think\model\relation\HasOne
     */
    public function coupon()
    {
        return $this->hasOne(StoreCouponUser::class, "id", "car_coupon_id");
    }

    /**
     * 一对一模型
     * @return \think\model\relation\HasOne
     */
    public function car()
    {
        return $this->hasOne(UserCar::class, "id", "car_id")->field("id, license_no");
    }

    /**
     * 获取where条件
     * @param $where
     * @param $model
     * @param string $aler
     * @param string $join
     * @return mixed
     */
    public static function getOrderWhere($where, $model, $aler = '', $join = '')
    {
        $model = $model->where('is_system_del', 0);
        if (isset($where['is_del']) && $where['is_del'] != '' && $where['is_del'] != -1) $model = $model->where($aler . 'is_del', $where['is_del']);
        if (isset($where['combination_id'])) {
            if ($where['combination_id'] == '普通订单') {
                $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
            }
            if ($where['combination_id'] == '拼团订单') {
                $model = $model->where($aler . 'combination_id', ">", 0)->where($aler . 'pink_id', ">", 0);
            }
            if ($where['combination_id'] == '秒杀订单') {
                $model = $model->where($aler . 'seckill_id', ">", 0);
            }
            if ($where['combination_id'] == '砍价订单') {
                $model = $model->where($aler . 'bargain_id', ">", 0);
            }
        }
        if (isset($where['pay_type'])) {
            switch ($where['pay_type']) {
                case 1:
                    $model = $model->where($aler . 'pay_type', 'weixin');
                    break;
                case 2:
                    $model = $model->where($aler . 'pay_type', 'yue');
                    break;
                case 3:
                    $model = $model->where($aler . 'pay_type', 'offline');
                    break;
            }
        }
        if (isset($where['type'])) {
            switch ($where['type']) {
                case 1:
                    $model = $model->where($aler . 'combination_id', 0)->where($aler . 'seckill_id', 0)->where($aler . 'bargain_id', 0);
                    break;
                case 2:
                    $model = $model->where($aler . 'combination_id', ">", 0);
                    break;
                case 3:
                    $model = $model->where($aler . 'seckill_id', ">", 0);
                    break;
                case 4:
                    $model = $model->where($aler . 'bargain_id', ">", 0);
                    break;
            }
        }

        if (isset($where['real_name']) && $where['real_name'] != '') {
            $model = $model->where($aler . 'order_id|' . $aler . 'real_name|' . $aler . 'user_phone' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
        }
        if (isset($where['data']) && $where['data'] !== '') {
            $model = self::getModelTime($where, $model, $aler . 'add_time');
        }
        return $model;
    }

}